package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Menus;
import com.guoguo.entity.vo.MenusVO;
import com.guoguo.service.MenuService;
import com.guoguo.utils.UUIDUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by KLSD on 2017/9/13.
 */
@Controller
@RequestMapping("/view")
public class MenuController extends BaseController {
    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/menuList", method = RequestMethod.GET)
    public ModelAndView menu(@RequestParam int pageNumber, @RequestParam int pageSize) {
        Map<String, Object> data = new HashedMap();
        PageBean<Menus> role = menuService.selectMenuList(pageNumber, pageSize);
        data.put("menuList", role);
        return returnView("/menuList", data);
    }

    /**
     * 去跳转添加修改菜单
     *
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/saveMenus", method = RequestMethod.GET)
    public ModelAndView saveMenus(String menuId) {
        Map<String, Object> data = new HashedMap();
        Map<Integer, String> menuTypes = new TreeMap<>();
        menuTypes.put(0, "一级菜单");
        menuTypes.put(1, "二级菜单");
        data.put("menuTypeList", menuTypes);
        List<Menus> menu = menuService.selectMenus();
        data.put("menuList", menu);
        if (menuId == null) {
            data.put("menuId", UUIDUtils.getUUID());
        } else {
            Menus menus = menuService.selectByPrimaryKey(menuId);
            data.put("menuId", menus.getMenuId());
            data.put("menuName", menus.getMenuName());
            data.put("menuDescription", menus.getMenuDescription());
            data.put("menuParent", menus.getMenuParent());
            data.put("menuType", menus.getMenuType());
            data.put("menuUrl", menus.getMenuUrl());
        }
        return returnView("/menu/menuAdd", data);
    }

    @ResponseBody
    @RequestMapping(value = "/saveOrupdateMenu", method = RequestMethod.POST)
    public JSONObject updateRole(@RequestParam String menuId, @RequestParam String menuName, @RequestParam String menuDescription,
                                 @RequestParam String menuUrl, @RequestParam(required = false) String menuParent,
                                 @RequestParam int menuType) {
        Menus menus = menuService.addMenu(menuId, menuName, menuDescription, menuUrl, menuParent, menuType);
        return returnResult(200, menus, "");
    }

    /**
     * 删除菜单
     *
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/deleteMenus", method = RequestMethod.GET)
    public ModelAndView delete(String menuId) {
        int i = menuService.deleteByPrimaryKey(menuId);
        PageBean<Menus> menus = menuService.selectMenuList(PaginationContext.getPageNum(), PaginationContext.getPageSize());
        Map<String, Object> data = new HashedMap();
        data.put("menuList", menus);
        return returnView("/menuList", data);
    }

    @ResponseBody
    @RequestMapping(value = "/getMenus", method = RequestMethod.POST)
    public JSONObject getMenus() {
        List<MenusVO> menu = menuService.getMenus();
        return null;
    }

}
